package com.renbang.action.devoloper;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.renbang.model.common.AjaxResult;
import com.renbang.model.common.Page;
import com.renbang.model.sys.SysLog;
import com.renbang.service.sys.SysLogServiceImp;
import com.renbang.utils.excel.ExcelExport;
@Controller
@RequestMapping(value = "developer/log")
public class SysLogAction {

	@Autowired
	SysLogServiceImp sysLogServiceImp;
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(HttpServletRequest request,SysLog sysLog,Page page) {
		Integer count = sysLogServiceImp.findTotal(sysLog);
		/*page.setCount(count);//查询总条数
		Double sum = page.getCount()+0.0;
		//点超标了返回第一页
		if(Math.ceil(sum/page.getSize())<page.getCurrpage()){
			page.setCurrpage(1);	
		}*/
		return new AjaxResult(AjaxResult.STATUS_OK, page.getCurrpage()+"",null ,sysLogServiceImp.findInPage(sysLog, page), count);
	}
	
	
	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(Integer id) {
		Integer num = sysLogServiceImp.removeById(id);
		return new AjaxResult(AjaxResult.STATUS_OK, null, "成功删除"+num+"条");
	}
	
	@RequestMapping(value = "/all")
	public @ResponseBody AjaxResult all() {
		Integer count = sysLogServiceImp.findTotal(null);
		return new AjaxResult(AjaxResult.STATUS_OK, null ,sysLogServiceImp.findByModel(null), count);
	}
	
	@RequestMapping(value = "/importExcel")
	public void importExcel(HttpServletRequest request,HttpServletResponse response) {
		response.setContentType("application/x-download");// 设置为下载application/x-download
		String dirPath = request.getSession().getServletContext().getRealPath("/")+ "fileupload";
		File fileDir = new File(dirPath);
		if (!fileDir.exists()||!fileDir.isDirectory()) {
			if (fileDir.mkdirs()) {
			}
		}
		List<SysLog> list = sysLogServiceImp.findByModel(null);
		ExcelExport<SysLog> excel = new ExcelExport<SysLog>();
		String[]headers = {"logId","userId","createTime","logContent","ip","userName"};
		  OutputStream out;
		try {
			String filedisplay = "日志.xlsx";
			String filedownload = dirPath+"/"+filedisplay;
			out = new FileOutputStream(filedownload);
			excel.exportExcel("日志列表", headers, list, out);
			out.close();  
			OutputStream out1 = response.getOutputStream();
			String filenamedisplay = URLEncoder.encode(filedisplay, "UTF-8");
			response.addHeader("Content-Disposition", "attachment;filename="
					+ filenamedisplay);
			FileInputStream io = new FileInputStream(filedownload);
			int i = 0;
			byte[] b = new byte[1024];
			while ((i = io.read(b)) > 0) {
				out1.write(b, 0, i);
			}
			// 6.关流
			io.close();
			out1.close();
			System.out.println("下载成功");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
	}
}
